package br.com.srv.componentes.jdbcdao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.ParseException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.simple.SimpleJdbcDaoSupport;

import br.com.srv.util.DateHelper;


public class DadosLidoJdbcDAOImpl extends SimpleJdbcDaoSupport implements DadosLidoJdbcDAO {

	public List<Map<String, String>> consultaPontosByVeiculo(
			String veiculoId, String dia, String horaInicial, String horaFinal) {						
		return consultaPontosByVeiculo(veiculoId, dia, dia, horaInicial, horaFinal);		
	}
	
	public List<Map<String, String>> consultaPontosByVeiculo(
			String veiculoId, String dataInicial, String dataFinal, String horaInicial, String horaFinal) {
		
		String query = "SELECT p.PTP_ID, p.PTP_DSC, d.DDL_PDIS, d.DDL_LNG, d.DDL_LAT, "
				+ " d.DDL_DH, d.DDL_VEL,d.DDL_BYT1, d.DDL_ODM, d.DDL_MOD, d.DDL_MOT_ID "
				+ " FROM dado_lido d, ponto p "
				+ " WHERE vei_id = ? AND DDL_DH > ? AND DDL_DH < ? "
				+ " AND d.ptp_id = p.ptp_id "
//				A linha abaixo � para ignoar pontos com a distancia maior que 1000 metros
				+ " AND ((d.DDL_ODM < 1000 AND d.DDL_BYT1 = 127) OR (d.DDL_ODM < 10 AND d.DDL_BYT1 = -127))";
				//as linhas a baixo s�o para levar em considera��o apenas os pontos que o veiculo esteja ligado
				//ou desligado com distancia do �ltimo ponto menor que 3
//				+ " AND ((DDL_BYT1 <> -127) OR (DDL_BYT1 = -127 AND DDL_ODM < 3) OR (DDL_BYT1 = -127 AND DDL_VEL > 10))";

		
		Object params[] = {veiculoId,tratarHoraParametro(dataInicial + " " + horaInicial), tratarHoraParametro(dataFinal + " " + horaFinal)};
		List<Map<String, String>> pontos = getJdbcTemplate().query(query, params, new PontosMapper());
		
		return pontos;
		
	}
	
	public String tratarHoraParametro(String dataHora) {
		
		String pattern = "";
		if (dataHora.split("-").length > 1) {
			pattern = DateHelper.DEFAULT_DATE_PATTERN_BD + DateHelper.DEFAULT_HOUR_PATTERN;
		} else {
			pattern = DateHelper.DEFAULT_DATE_PATTERN + DateHelper.DEFAULT_HOUR_PATTERN;
		}
		
		
		
		try {
			Date date = DateHelper.parseDate(pattern, dataHora);

			date = DateHelper.addHour(date, 3);
			
			dataHora = DateHelper.parseString("yyyy-MM-dd " + DateHelper.DEFAULT_HOUR_PATTERN, date);
		} catch (ParseException e) {
			System.out.println("0035 : aqui");
		}
		return dataHora;
	}
	
    private static class PontosMapper implements RowMapper {

		public Map<String, String> mapRow(ResultSet rs, int rowNum) throws SQLException {
			Map<String, String> ponto = new HashMap<String, String>();
			ponto.put("NEAR_ID", rs.getString("PTP_ID"));
			ponto.put("PTP_DSC", rs.getString("PTP_DSC"));			
			ponto.put("DATE_TIME", DateHelper.ajustarHora(rs.getTimestamp("DDL_DH"), -3));
			ponto.put("NEAR_DESC", rs.getString("PTP_DSC"));
			ponto.put("NEAR_DIST", rs.getString("DDL_PDIS"));
			ponto.put("Y", rs.getString("DDL_LAT"));
			ponto.put("X", rs.getString("DDL_LNG"));
			ponto.put("VEL", rs.getString("DDL_VEL"));
			ponto.put("IGNITION", rs.getString("DDL_BYT1"));
			ponto.put("DIST_BETWEEN_POSITIONS", rs.getString("DDL_ODM"));
			ponto.put("COUNT", rs.getString("DDL_MOD"));
			ponto.put("MOTORISTA", rs.getString("DDL_MOT_ID"));
			
			return ponto;
		}

    }

}
